package cn.edu.lnsf.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

import java.util.Vector;

import cn.edu.lnsf.model.Customers;
import cn.edu.lnsf.model.Orders;
import cn.edu.lnsf.service.IcustomersService;
import cn.edu.lnsf.service.impl.CustomersServiceImpl;


public class CustomersController {

	// 注入controller Service
	@SuppressWarnings("unused")
	private IcustomersService customersService;

	public void setIcustomersService(IcustomersService customersService) {
		this.customersService = customersService;
	}

	Customers c = new Customers();
	Scanner scanner = new Scanner(System.in);
	IcustomersService Service = new CustomersServiceImpl();

	// ----------------------------根据顾客身份证查找顾客信息------------------------
	public void findCustomersBycustomerIDCard(String customerID) {

//		Vector<Customers> v = new Vector<Customers>();

		System.out.print("\t\t\t\t❤请输入查找的顾客身份证号：");
		String customerIDCard = scanner.next();
		c.setCustomerIDCard(customerIDCard);
		Service.findCustomersBycustomerIDCard(customerIDCard);
//		v = Service.findCustomersBycustomerIDCard(customerIDCard);

	}

	// ----------------------------查找所有顾客信息------------------------

	public void findAllCustomers() {
//		Vector<Customers> v = new Vector<Customers>();
//		v = Service.findAllCustomers();
		Service.findAllCustomers();
	}

	// ------------------------------插入顾客信息-------------------------------------------
	public void insertintofoCustomers() {
        Orders o = new Orders();
        OrdersController ordersCon = new OrdersController();
		System.out.print("\t\t\t\t❤请输入身份证号：");
		String customerIDCard = scanner.next();
		if(Service.findCustomersByID(customerIDCard)==true){
			System.out.println();
			System.out.println("\t\t\t\t\t\t\t✪    *^▽^*顾客已存在！可直接预订房间！ ✪");	
			System.out.println();
			Vector<Customers> v = new Vector<Customers>();
			v = Service.findCustomersBycustomerIDCard(customerIDCard);
			for(Customers c : v){
				System.out.println("\t\t\t\t\t\t\t\t❤   顾客信息  ❤");
				System.out.print("\t\t\t\t\t\t\t\t  ☆ 性别：");	
				String customerGender = c.getCustomerGender();
				System.out.println(customerGender);
				
				System.out.print("\t\t\t\t\t\t\t\t  ☆ 姓名：");
				String customerName = c.getCustomerName();
				System.out.println(customerName);
				
				System.out.print("\t\t\t\t\t\t\t\t  ☆ 顾客生日：");
				Date customerBirthday = c.getCustomerBirthday();
				System.out.println(customerBirthday);
				
				System.out.print("\t\t\t\t\t\t\t\t  ☆ 联系方式:");
				String customerPhone = c.getCustomerPhone();
				System.out.println(customerPhone);
				
				System.out.print("\t\t\t\t\t\t\t\t  ☆ 备注:");
				String remarks = c.getRemarks();
				System.out.println(remarks);
				
				c.setCustomerIDCard(customerIDCard);
				c.setCustomerBirthday(customerBirthday);
				c.setCustomerGender(customerGender);
				c.setCustomerName(customerName);
				c.setCustomerPhone(customerPhone);
				c.setRemarks(remarks);
			}	
			o.setCustomerIDCard(customerIDCard);
			ordersCon.insertOrders(o);

		}
		else{
			System.out.println();
			System.out.println("\t\t\t\t\t\t\t  ✪       欢迎预订!请填写相关信息：     ✪");
			System.out.println();
			System.out.print("\t\t\t\t\t\t\t\t      ☆请输入性别：");
			String customerGender;
			if(Integer.parseInt(customerIDCard.substring(16,17)) % 2 == 0){
				customerGender = "女";
			}else{
				customerGender = "男";
			}		
			System.out.println(customerGender);
//			String customerGender = scanner.next();
			System.out.print("\t\t\t\t\t\t\t\t      ☆请输入姓名：");
			String customerName = scanner.next();
			System.out.print("\t\t\t\t\t\t\t\t      ☆请输入顾客生日：");
			String customerBirthday = customerIDCard.substring(6, 10)+"-"+
					customerIDCard.substring(10, 12)+"-"+customerIDCard.substring(12, 14);
			System.out.println(customerBirthday);
//			String customerBirthday = scanner.next();
			System.out.print("\t\t\t\t\t\t\t\t      ☆请输入联系方式:");
			String customerPhone = scanner.next();
			System.out.print("\t\t\t\t\t\t\t\t      ☆备注:");
			String remarks = scanner.next();
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			try {
				Date date = format.parse(customerBirthday);
				//System.out.println(date.getTime());
				c.setCustomerBirthday(date);
			} catch (ParseException e) {
				System.out.println("\t\t\t\t\t\t\t\t  ✪       不合法的输入     ✪");
				e.printStackTrace();
			}
			c.setCustomerIDCard(customerIDCard);
			c.setCustomerGender(customerGender);
			c.setCustomerName(customerName);
			c.setCustomerPhone(customerPhone);
			c.setRemarks(remarks);
			if (Service.insertintofoCustomers(c) == true)
				System.out.println("\t\t\t\t\t\t\t\t  ✪       插入成功！     ✪");
			else
				System.out.println("\t\t\t\t\t\t\t\t  ✪       请重新输入!   ✪");
			o.setCustomerIDCard(customerIDCard);
			ordersCon.insertOrders(o);
		}	
	}
	
	
	public void insertCustomers() {
        Orders o = new Orders();
        OrdersController ordersCon = new OrdersController();
		System.out.print("\t\t\t\t❤请输入身份证号：");
		String customerIDCard = scanner.next();
		if(Service.findCustomersByID(customerIDCard)==true){
			System.out.println("\t\t\t\t\t\t\t✪       该住客信息已经存在...   ✪ ");
		}
		else{
			System.out.println();
			System.out.println("\t\t\t\t\t\t\t  ✪       请填写相关信息：     ✪");
			System.out.println();
			System.out.print("\t\t\t\t\t\t\t\t      ☆请输入性别：");
			String customerGender;
			if(Integer.parseInt(customerIDCard.substring(16,17)) % 2 == 0){
				customerGender = "女";
			}else{
				customerGender = "男";
			}		
			System.out.println(customerGender);
//			String customerGender = scanner.next();
			System.out.print("\t\t\t\t\t\t\t\t      ☆请输入姓名：");
			String customerName = scanner.next();
			System.out.print("\t\t\t\t\t\t\t\t      ☆请输入顾客生日：");
			String customerBirthday = customerIDCard.substring(6, 10)+"-"+
					customerIDCard.substring(10, 12)+"-"+customerIDCard.substring(12, 14);
			System.out.println(customerBirthday);
//			String customerBirthday = scanner.next();
			System.out.print("\t\t\t\t\t\t\t\t      ☆请输入联系方式:");
			String customerPhone = scanner.next();
			System.out.print("\t\t\t\t\t\t\t\t      ☆备注:");
			String remarks = scanner.next();
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			try {
				Date date = format.parse(customerBirthday);
				//System.out.println(date.getTime());
				c.setCustomerBirthday(date);
			} catch (ParseException e) {
				System.out.println("\t\t\t\t\t\t\t\t  ✪       不合法的输入     ✪");
				e.printStackTrace();
			}
			c.setCustomerIDCard(customerIDCard);
			c.setCustomerGender(customerGender);
			c.setCustomerName(customerName);
			c.setCustomerPhone(customerPhone);
			c.setRemarks(remarks);
			if (Service.insertintofoCustomers(c) == true)
				System.out.println("\t\t\t\t\t\t\t\t  ✪       插入成功！     ✪");
			else
				System.out.println("\t\t\t\t\t\t\t\t  ✪       请重新输入!   ✪");

		}	
	}

	// ---------------------------------更新顾客信息------------------------------------------
	public void updateCustomers(Customers c) {
		System.out.print("\t\t\t\t❤请输入需更新的顾客身份证号：");
		String customerIDCard = scanner.next();
		System.out.println();
		System.out.print("\t\t\t\t❤请依次输入修改后的顾客性别：");
		String customerGender = scanner.next();
		System.out.println();
		System.out.print("\t\t\t\t❤请依次输入修改后的顾客姓名：");
		String customerName = scanner.next();
		System.out.println();
		System.out.print("\t\t\t\t❤请依次输入修改后的顾客出生日期：");
		String customerBirthday = scanner.next();
		System.out.println();
		System.out.print("\t\t\t\t❤请依次输入修改后的顾客电话号码：");
		String customerPhone = scanner.next();
		System.out.println();
		System.out.print("\t\t\t\t❤请依次输入修改后的备注：");
		String remarks = scanner.next();
		System.out.println();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date = format.parse(customerBirthday);
			c.setCustomerBirthday(date);
		} catch (ParseException e) {
			System.out.println("\t\t\t\t\t\t\t\t  ✪       不合法的输入      ✪");
			e.printStackTrace();
		}
		
		c.setCustomerIDCard(customerIDCard);
		c.setCustomerGender(customerGender);
		c.setCustomerName(customerName);
		c.setCustomerPhone(customerPhone);
		c.setRemarks(remarks);
		if (Service.updateCustomers(c) == true)
			System.out.println("\t\t\t\t\t\t\t\t  ✪       更新成功！      ✪");
		else
			System.out.println("\t\t\t\t\t\t\t\t  ✪       请重新输入!   ✪");

	}

	// -----------------------------------删除一条顾客信息--------------------------------------
	public void deleteCustomers(String customerID) {

		System.out.print("\t\t\t\t❤请输入需要删除的顾客身份证号：");
		String customerIDCard = scanner.next();
		c.setCustomerIDCard(customerIDCard);
		if (Service.deleteCustomers(customerIDCard) == true)
			System.out.println("\t\t\t\t\t\t\t\t  ✪       删除成功！   ✪");
		else
			System.out.println("\t\t\t\t\t\t\t\t  ✪       请重新输入!   ✪");

	}

}
